package net.qhzw.vip.qiyu.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.qhzw.vip.qiyu.entity.GuaranteeCompany;
import net.qhzw.vip.qiyu.model.BoxVo;
import net.qhzw.vip.qiyu.model.GuaranteeCompanyUserVo;
import net.qhzw.vip.qiyu.result.JsonResult;
import net.qhzw.vip.qiyu.service.GuaranteeCompanyService;
import net.qhzw.vip.qiyu.util.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("sys/guarantee/company")
@Api(tags = "担保公司控制器")
public class SysGuaranteeCompanyController {


    @Autowired
    private GuaranteeCompanyService guaranteeCompanyService;


    @ApiOperation(value = "分页查询担保公司")
    @GetMapping
    public JsonResult<Page<GuaranteeCompany>> findByPage(Page<GuaranteeCompany> page, String name, Byte status) {
        Long shopId = SecurityUtils.getLoginUser().getShopId();
        Page<GuaranteeCompany> companyPage = guaranteeCompanyService.findByPage(page, shopId, name, status);
        return JsonResult.ok(companyPage);
    }

    @ApiOperation(value = "根据ID查询担保公司")
    @GetMapping("{id}")
    public JsonResult<GuaranteeCompany> findGuaranteeCompany(@PathVariable Long id) {
        GuaranteeCompany guaranteeCompany = guaranteeCompanyService.getById(id);
        return JsonResult.ok(guaranteeCompany);
    }


    @ApiOperation(value = "添加担保公司")
    @PostMapping
    public JsonResult addGuaranteeCompany(@RequestBody GuaranteeCompany guaranteeCompany) {
        Long shopId = SecurityUtils.getLoginUser().getShopId();
        guaranteeCompany.setShopId(shopId);
        boolean save = guaranteeCompanyService.addGuaranteeCompany(guaranteeCompany);
        if (save) {
            return JsonResult.ok();
        }
        return JsonResult.fail();
    }

    @ApiOperation(value = "修改担保公司")
    @PutMapping
    public JsonResult updateGuaranteeCompany(@RequestBody GuaranteeCompany guaranteeCompany) {
        boolean update = guaranteeCompanyService.updateById(guaranteeCompany);
        if (update) {
            return JsonResult.ok();
        }
        return JsonResult.fail();
    }


    @ApiOperation(value = "设置担保公司状态")
    @PutMapping("setStatus")
    public JsonResult setStatus(@RequestBody GuaranteeCompany guaranteeCompany) {
        boolean save = guaranteeCompanyService.updateById(guaranteeCompany);
        if (save) {
            return JsonResult.ok();
        }
        return JsonResult.fail();
    }


    @ApiOperation(value = "删除担保公司")
    @DeleteMapping("{id}")
    public JsonResult delGuaranteeCompany(@PathVariable String id) {
        List<String> list = Arrays.asList(id.split(","));
        boolean remove = guaranteeCompanyService.removeByIds(list);
        if (remove) {
            return JsonResult.ok();
        }
        return JsonResult.fail();
    }

    @ApiOperation(value = "查询担保公司下拉框")
    @GetMapping("selectBox/{status}")
    public JsonResult selectBox(@PathVariable Byte status) {
        Long shopId = SecurityUtils.getLoginUser().getShopId();
        List<GuaranteeCompany> list = guaranteeCompanyService.list(new LambdaQueryWrapper<GuaranteeCompany>()
                .eq(shopId != 1, GuaranteeCompany::getShopId, shopId)
                .eq(GuaranteeCompany::getStatus, status));
        List<BoxVo> boxs = new ArrayList<>();
        if (!CollectionUtils.isEmpty(list)) {
            for (GuaranteeCompany guaranteeCompany : list) {
                boxs.add(new BoxVo(guaranteeCompany.getId(), guaranteeCompany.getName()));
            }
        }
        return JsonResult.ok(boxs);
    }


    @ApiOperation(value = "根据用户id查询担保公司")
    @GetMapping("getGuaranteeCompany")
    public JsonResult<GuaranteeCompanyUserVo> getGuaranteeCompanyByUserId(@RequestParam Long userId, Byte status) {
        GuaranteeCompanyUserVo guaranteeCompany = guaranteeCompanyService.getGuaranteeCompanyByUserId(userId, status);
        return JsonResult.ok(guaranteeCompany);
    }


}
